package com.panshi.controller;

import com.panshi.api.SystemUserApi;
import com.panshi.po.SystemUserPo;
import com.panshi.vo.SystemUserUpdateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
public class SysEditController {

    @Resource
    private SystemUserApi systemUserApi;

    @GetMapping("/sys/edit")
    public String editPage(@RequestParam Integer id, Model model){

        SystemUserPo userPo = systemUserApi.queryById(id).getData();

        log.info("userPo-----------------------{}",userPo);

        model.addAttribute("user",userPo);

        return "layuiIndex/user-edit";
    }

    @PostMapping("/sys/edit")
    @ResponseBody
    public Map<String,Object> edit(SystemUserUpdateVo updateVo){

        Map<String,Object> map = new HashMap<>();

        updateVo.setUpdatedate(new Date());

        Integer state = systemUserApi.updateUser(updateVo).getData();

        if(state == 0){
            map.put("success",false);
            map.put("message","修改失败，请重新修改");
        }else if(state == 1){
            map.put("success",true);
            map.put("message","修改成功");
        }

        return map;
    }

    @PostMapping("/sys/del")
    @ResponseBody
    public Map<String,Object> del(SystemUserUpdateVo updateVo){

        Map<String,Object> map = new HashMap<>();

        updateVo.setUpdatedate(new Date());

        Integer state = systemUserApi.updateUser(updateVo).getData();

        if(state == 0){
            map.put("success",false);
            map.put("message","删除失败");
        }else if(state == 1){
            map.put("success",true);
            map.put("message","删除成功");
        }
        return map;

    }

}
